
#include "test4.h"

#define SCREEN_WIDTH  480
#define SCREEN_HEIGHT 320

// 页面数量
#define PAGE_COUNT 4

// 页面颜色数组
static const lv_color_t page_colors[PAGE_COUNT] = {
    LV_COLOR_MAKE(0x40, 0x70, 0xFF),  // 蓝色
    LV_COLOR_MAKE(0xFF, 0x68, 0x40),  // 橙色
    LV_COLOR_MAKE(0x40, 0xFF, 0x70),  // 绿色
    LV_COLOR_MAKE(0xFF, 0x40, 0x70)   // 粉色
};

static lv_obj_t *main_cont;  // 主容器
static lv_obj_t *pages[PAGE_COUNT];  // 页面数组
static lv_indev_t *indev;  // 输入设备
static lv_point_t last_point;  // 记录上一次触摸点
static lv_dir_t scroll_dir = LV_DIR_NONE;  // 当前滚动方向

// 事件处理回调
static void event_handler(lv_event_t *e) {
    lv_obj_t *obj = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_PRESSING) {
        lv_indev_t *indev = lv_indev_get_act();
        if (indev) {
            lv_point_t point;
            lv_indev_get_point(indev, &point);
            
            if (last_point.x == 0 && last_point.y == 0) {
                last_point = point;
                return;
            }
            
            int16_t diff_x = abs(point.x - last_point.x);
            int16_t diff_y = abs(point.y - last_point.y);
            
            // 确定滑动方向
            if (scroll_dir == LV_DIR_NONE) {
                if (diff_x > 10 || diff_y > 10) {
                    scroll_dir = (diff_x > diff_y) ? LV_DIR_HOR : LV_DIR_VER;
                    lv_obj_set_scroll_dir(main_cont, scroll_dir);
                }
            }
            
            last_point = point;
        }
    } else if (code == LV_EVENT_RELEASED || code == LV_EVENT_SCROLL_END) {
        // 重置滑动状态
        last_point.x = 0;
        last_point.y = 0;
        scroll_dir = LV_DIR_NONE;
        lv_obj_set_scroll_dir(main_cont, LV_DIR_HOR | LV_DIR_VER);
    }
}
// 创建页面
static void create_pages(void) {
    // 设置主容器样式
    main_cont = lv_obj_create(lv_scr_act());

    lv_obj_set_pos(main_cont, 0, 0);
    lv_obj_set_size(main_cont, SCREEN_WIDTH * PAGE_COUNT, SCREEN_HEIGHT);

    lv_obj_set_style_bg_color(main_cont, lv_color_black(), 0);
    lv_obj_set_style_bg_opa(main_cont, LV_OPA_COVER, 0);
    // lv_obj_set_flex_flow(main_cont, LV_FLEX_FLOW_ROW);
    // lv_obj_set_scroll_snap_x(main_cont, LV_SCROLL_SNAP_CENTER);
    // lv_obj_set_scroll_snap_y(main_cont, LV_SCROLL_SNAP_CENTER);
    // lv_obj_set_scroll_dir(main_cont, LV_DIR_HOR | LV_DIR_VER);
    lv_obj_set_scrollbar_mode(main_cont, LV_SCROLLBAR_MODE_OFF);

    lv_obj_set_style_pad_all(main_cont,0,0);

    lv_obj_add_event_cb(main_cont, event_handler, LV_EVENT_ALL, NULL);

    
    // 创建各个页面
    for (int i = 0; i < PAGE_COUNT; i++) {
        pages[i] = lv_obj_create(main_cont);
        lv_obj_set_size(pages[i], SCREEN_WIDTH, SCREEN_HEIGHT);
        lv_obj_set_style_bg_color(pages[i], page_colors[i], 0);
        lv_obj_set_style_bg_opa(pages[i], LV_OPA_COVER, 0);
        lv_obj_set_style_border_width(pages[i], 0, 0);
        lv_obj_set_style_radius(pages[i], 0, 0);

        lv_obj_set_pos(pages[i], i*SCREEN_WIDTH, 0);

        lv_obj_set_style_pad_all(pages[i],0,0);
        
        // 添加页面标签
        lv_obj_t *label = lv_label_create(pages[i]);
        lv_label_set_text_fmt(label, "Page %d", i + 1);
        lv_obj_center(label);
    }
}





void test4_setup_ui(void) {
    // 创建主容器

    
    // 创建页面
    create_pages();

    lv_obj_scroll_to_x(main_cont, 0, LV_ANIM_OFF);
}
